<?php
/*
Plugin Name: Simple Related Posts
Description: Displays a selected number of posts from the same current category.
Author: <a href="http://www.fubra.com">Ray Viljoen</a>
Version: 1.0
Plugin URI: http://catn.com/community/plugins/random-related-posts/
Usage: widget.
*/
// � 2009-2010 Fubra Limited, all rights reserved.

class RPWidget extends WP_Widget {
	var $prefix;
	var $textdomain;
	
	/** constructor */
	function RPWidget() {
		$this->prefix = 'cv';
		$this->textdomain = hybrid_get_textdomain();	
		
		$widget_ops = array('classname' => 'widget-popular', 'description' => __('Random related posts.', $this->textdomain) );
		$this->WP_Widget("{$this->prefix}-related-post-widget", __('Related posts', $this->textdomain), $widget_ops); 		
	}

	function widget($args, $instance) {
		extract( $args );
		
		echo $before_widget;

		/* If there is a title given, add it along with the $before_title and $after_title variables. */
		$title = $instance['title'];
		if ( $title) {
			$title =  apply_filters( 'widget_title',  $title, $instance, $this->id_base );
			$title = str_replace('related', '<span>related<span>r</span></span>', $title);
			echo $before_title . $title . $after_title;
		}
		
		$num_posts = $instance['number'];
		global $post;

		$cat = get_the_category( $post->ID );
		if(array_key_exists(0, $cat))
			{$cat = $cat[0]->cat_ID;}else{$cat = '';}

		?>	
		
		<div class="random-related-posts popular-posts">
			<ul>
				<?php //rewind_posts(); ?>
				<?php cv_radom_related_posts($num_posts, $cat); ?>
			</ul>	
		</div>	
		
		<?php
		echo $after_widget;
	}

	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] = strip_tags($new_instance['number']);
		return $instance;
	}

	function form($instance) {
		$defaults = array(
			'title' => 'Related posts',			
			'number' => '5'		
		);	
		$instance = wp_parse_args( (array) $instance, $defaults );   
		
		if(array_key_exists('title', $instance)){
			$title = esc_attr($instance['title']);
		}else{$title='';}

		if(array_key_exists('number', $instance)){
			$number = esc_attr($instance['number']);
		}else{$number=3;}
?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
            <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number:'); ?>
              <select id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" value="<?php echo $number; ?>" >
             <?php
		for ( $i = 1; $i <= 20; ++$i )
			echo "<option value='$i' " . ( $number == $i ? "selected='selected'" : '' ) . ">$i</option>";
?>
              </select></label></p>
        <?php
	}
}
add_action('widgets_init', create_function('', 'return register_widget("RPWidget");'));